package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.Discussreply;
import cn.sunlelin.educationserver.mapper.DiscussMapper;
import cn.sunlelin.educationserver.mapper.DiscussreplyMapper;
import cn.sunlelin.educationserver.service.DiscussreplyService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DiscussreplyServiceImpl implements DiscussreplyService {
    @Autowired
    private DiscussreplyMapper discussreplyMapper;
    @Autowired
    private DiscussMapper discussMapper;

    @Override
    public boolean createDiscussreply(Discussreply discussreply) {
        discussreply.setCreateTime(new Date());
        discussreplyMapper.createDiscussreply(discussreply);
        discussMapper.addReplyNum(discussreply.getDiscussId());
        return true;
    }

    @Override
    public Map<String, Object> selectDiscussreplyById(Integer discussreplyId) {
        return discussreplyMapper.selectDiscussreplyById(discussreplyId);
    }

    @Override
    public List<Map<String, Object>> selectDiscussreplyByUserId(Integer userId) {
        return discussreplyMapper.selectDiscussreplyByUserId(userId);
    }

    @Override
    public Page<Map<String,Object>> getPageByUserId(Integer currentPage, Integer pageSize, Integer userId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> discussreplyList=discussreplyMapper.selectDiscussreplyByUserId(userId);
        return (Page<Map<String, Object>>) discussreplyList;
    }

    @Override
    public List<Map<String, Object>> selectDiscussreplyByDiscussId(Integer discussId) {
        return discussreplyMapper.selectDiscussreplyByDiscussId(discussId);
    }

    @Override
    public Page<Map<String,Object>> getPageByDiscussId(Integer currentPage, Integer pageSize, Integer discussId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> discussreplyList=discussreplyMapper.selectDiscussreplyByDiscussId(discussId);
        return (Page<Map<String, Object>>) discussreplyList;
    }

    @Override
    public boolean deleteDiscussreply(Integer discussreplyId) {
        if(discussreplyMapper.selectDiscussreplyById(discussreplyId)==null)
            return false;
        Discussreply discussreply=new Discussreply();
        discussreply.setDiscussreplyId(discussreplyId);
        discussreply.setUpdateTime(new Date());
        discussreplyMapper.deleteDiscussreply(discussreply);
        return true;
    }
}
